<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="事项名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入事项名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:matter:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:matter:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:matter:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:matter:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="matterList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="事项名称" align="center" prop="name" />
      <el-table-column label="图标" align="center" prop="icon" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.icon" :width="50" :height="50" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="事项说明" align="center" prop="explain" /> -->
      <el-table-column label="所属专区" align="center" prop="typeId" >
        <template slot-scope="scope">
          <el-tag>{{scope.row.zoneType.zoneTitle}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否上架" align="center" prop="isShelve">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isShelve"
            active-color="#13ce66"
            :active-value="1"
            :inactive-value="0"
            inactive-color="#999"
            @change="changeisDis(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:matter:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:matter:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改办理事项信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="所属专区" prop="typeId">
          <el-cascader
            v-model="form.typeId"
            :options="typeOptions"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="业务类型" prop="choice">
          <el-radio-group v-model="form.choice">
            <el-radio label="1">查询</el-radio>
            <el-radio label="2">引导</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否跳转第三方" prop="isJump">
          <el-radio-group v-model="form.isJump">
            <el-radio label="0">否</el-radio>
            <el-radio label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="跳转引导" prop="linkText" v-if="form.isJump==1">
          <el-input v-model="form.linkText" placeholder="请输入跳转引导文本" />
        </el-form-item>
        <el-form-item label="小程序名" prop="appName" v-if="form.isJump==1">
          <el-input v-model="form.appName" placeholder="请输入小程序名" />
        </el-form-item>
        <el-form-item label="小程序APPID" prop="appId" v-if="form.isJump==1">
          <el-input v-model="form.appId" placeholder="请输入小程序appId" />
        </el-form-item>
        <el-form-item label="小程序内部链接" prop="appLink" v-if="form.isJump==1">
          <el-input v-model="form.appLink" placeholder="请输入小程序内部链接" />
        </el-form-item>
        <el-form-item label="事项名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入事项名称" />
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <image-upload v-model="form.icon" />
        </el-form-item>
        <el-form-item label="事项说明" prop="explain">
          <el-input
            v-model="form.explain"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="办理指引" prop="guide">
          <el-input v-model="form.guide" placeholder="请输入办理指引" />
        </el-form-item>
        <el-form-item label="受理条件"></el-form-item>
        <el-row>
          <div style="margin: 10px;">
            <el-table :data="acceptance" style="width: 100%">
              <el-table-column label="内容">
                <template #default="scope">
                  <el-input
                    size="large"
                    v-model.trim="scope.row.value"
                    placeholder="请输入内容"
                    style="width: 100%"
                  />
                </template>
              </el-table-column>
              <el-table-column label="附件">
                <template #default="scope">
                <!-- <el-upload
                    :file-list="fileList"
                    class="upload-demo"
                    :action="api_host + '/common/upload'"
                    :limit="1"
                    :on-success="(file, uploadFiles) => { handleSuccess(file, uploadFiles,scope.$index) }"
                    :on-exceed="handleExceed"
                  >
                  <el-button
                    size="mini"
                    plain
                    >附件上传</el-button
                  >
                  </el-upload> -->
                  <el-upload
                    class="upload-demo"
                    :show-file-list="false"
                    :action="api_host + '/common/upload'"
                    :limit="1"
                    :on-success="(file, uploadFiles) => { handleSuccess(file, uploadFiles,scope.$index) }"
                  >
                  <el-button
                    size="mini"
                    plain
                    >附件上传</el-button
                  >
                  <p>{{scope.row.fileLink}}</p>
                  </el-upload>
                </template>
              </el-table-column>
              <el-table-column label="操作">
                <template #default="scope">
                  <el-button
                    type="danger"
                    size="mini"
                    plain
                    @click.prevent="btnContent(1, scope.$index)"
                    v-if="acceptance.length>1"
                    >删除</el-button
                  >
                  <el-button
                    type="success"
                    size="mini"
                    plain
                    @click.prevent="btnContent(2, scope.$index)"
                    v-if="scope.$index==(acceptance.length-1)"
                    >新增</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-row>
        <el-form-item label="申请资料"></el-form-item>
        <el-row>
          <div style="margin: 10px;">
            <el-table :data="application" style="width: 100%">
              <el-table-column label="内容">
                <template #default="scope">
                  <el-input
                    size="large"
                    v-model.trim="scope.row.value"
                    placeholder="请输入内容"
                    style="width: 100%"
                  />
                </template>
              </el-table-column>
              <el-table-column label="附件">
                <template #default="scope">
                  <el-upload
                    class="upload-demo"
                    :action="api_host + '/common/upload'"
                    :limit="1"
                    :on-success="(file, uploadFiles) => { handleSuccess2(file, uploadFiles,scope.$index) }"
                    
                  >
                  <el-button
                    size="mini"
                    plain
                    >附件上传</el-button
                  >
                  <p>{{scope.row.fileLink}}</p>
                  </el-upload>
                </template>
              </el-table-column>
              <el-table-column label="操作">
                <template #default="scope">
                  <el-button
                    type="danger"
                    size="mini"
                    plain
                    @click.prevent="btnContent2(1, scope.$index)"
                    v-if="application.length>1"
                    >删除</el-button
                  >
                  <el-button
                    type="success"
                    size="mini"
                    plain
                    @click.prevent="btnContent2(2, scope.$index)"
                    v-if="scope.$index==(application.length-1)"
                    >新增</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-row>
        <el-form-item label="办理流程"></el-form-item>
        <el-row>
          <div style="margin: 10px;">
            <el-table :data="handle" style="width: 100%">
              <el-table-column label="内容">
                <template #default="scope">
                  <el-input
                    size="large"
                    v-model.trim="scope.row.value"
                    placeholder="请输入内容"
                    style="width: 100%"
                  />
                </template>
              </el-table-column>
              <el-table-column label="附件">
                <template #default="scope">
                  <el-upload
                    class="upload-demo"
                    :action="api_host + '/common/upload'"
                    :limit="1"
                    :on-success="(file, uploadFiles) => { handleSuccess3(file, uploadFiles,scope.$index) }"
                    
                  >
                  <el-button
                    size="mini"
                    plain
                    >附件上传</el-button
                  >
                  <p>{{scope.row.fileLink}}</p>
                  </el-upload>
                </template>
              </el-table-column>
              <el-table-column label="操作">
                <template #default="scope">
                  <el-button
                    type="danger"
                    size="mini"
                    plain
                    @click.prevent="btnContent3(1, scope.$index)"
                    v-if="handle.length>1"
                    >删除</el-button
                  >
                  <el-button
                    type="success"
                    size="mini"
                    plain
                    @click.prevent="btnContent3(2, scope.$index)"
                    v-if="scope.$index==(handle.length-1)"
                    >新增</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMatter,
  getMatter,
  delMatter,
  addMatter,
  updateMatter,
} from "@/api/system/matter";
import { listType, getType, delType, addType, updateType } from "@/api/system/zoneType";

export default {
  name: "Matter",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 办理事项信息表格数据
      matterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        typeId: null,
        isShelve: null,
        choice:null
      },
      // 表单参数
      form: {},
      typeOptions:[], //专区信息列表
      acceptance:[{value:'',fileLink:''}], //受理条件
      application:[{value:'',fileLink:''}],
      handle:[{value:'',fileLink:''}],
      // isShow:false,
      fileList:[],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "事项名称不能为空", trigger: "blur" },
        ],
        icon: [{ required: true, message: "图标不能为空", trigger: "blur" }],
        explain: [
          { required: true, message: "事项说明不能为空", trigger: "blur" },
        ],
        typeId: [
          { required: true, message: "专区ID不能为空", trigger: "change" },
        ],
        isShelve: [
          {
            required: true,
            message: "是否上架  0下架  1上架不能为空",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getList2();
  },
  methods: {
    /** 查询办理事项信息列表 */
    getList() {
      this.loading = true;
      listMatter(this.queryParams).then((response) => {
        this.matterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询办事专区信息列表 */
    getList2() {
      this.loading = true;
      listType(this.queryParams).then(response => {
        // this.typeOptions = response.rows
        response.rows.forEach((item,index)=>{
          this.typeOptions.push({
            value:item.id,
            label:item.zoneTitle,
            children:[]
          })
          item.children.forEach(item2=>{
            if(item2.pid==this.typeOptions[index].value){
              this.typeOptions[index].children.push({
                value:item2.id,
                label:item2.zoneTitle
              })
            }
          })
        })
        this.loading = false;
      });
    },
    handleChange(value) {
      console.log(value[1])
      this.form.typeId = value[1]
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        icon: null,
        explain: null,
        guide: null,
        acceptance: null,
        application: null,
        handle: null,
        typeId: null,
        isShelve: null,
        choice:null,
        isJump:null,
        linkText:null,
        appName:null,
        appId:null,
        appLink:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.acceptance = [{value:'',fileLink:''}]
      this.application = [{value:'',fileLink:''}]
      this.handle = [{value:'',fileLink:''}]
      this.open = true;
      this.title = "添加办理事项信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.fileList = []
      this.acceptance = [{value:'',fileLink:''}]
      this.application = [{value:'',fileLink:''}]
      this.handle = [{value:'',fileLink:''}]
      const id = row.id || this.ids;
      getMatter(id).then((response) => {
        this.form = response.data;
        this.form.choice = response.data.choice.toString()
        this.form.isJump = response.data.isJump.toString()
        if(response.data.acceptanceObj!=null){
          this.acceptance = response.data.acceptanceObj
        }
        if(response.data.applicationObj!=null){
          this.application = response.data.applicationObj
        }
        if(response.data.handleObj!=null){
          this.handle = response.data.handleObj
        }
        
          // response.data.acceptanceObj.forEach(item=>{
          //   console.log(item.fileLink);
          //   this.fileList.push({
          //     name:item.fileLink
          //   })
          // })
        // if(response.data.acceptanceObj.length>1){
        //   this.acceptance = response.data.acceptanceObj
        //   response.data.acceptanceObj.forEach(item=>{
        //     this.fileList.push({
        //       name:item.fileLink
        //     })
        //   })
        // }
        this.open = true;
        this.title = "修改办理事项信息";
      });
    },
    //修改是否上架状态
    changeisDis(row) {
      updateMatter(row).then((res) => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.acceptanceObj = this.acceptance
            this.form.applicationObj = this.application
            this.form.handleObj = this.handle
            updateMatter(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.$router.go(0);
            });
          } else {
            addMatter(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除办理事项信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delMatter(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/matter/export",
        {
          ...this.queryParams,
        },
        `matter_${new Date().getTime()}.xlsx`
      );
    },
    btnContent(type, index) {
      if (type == 2) {
        this.acceptance.push({
          value:'',
          fileLink:''
        })
      } else {
        this.acceptance.splice(index, 1);
      }
    },
    handleSuccess (file, uploadFiles,index) {
        console.log(file, 111);
        console.log(index);
        this.acceptance[index].fileLink = file.fileName
    },
    btnContent2(type, index) {
      if (type == 2) {
        this.application.push({
          value:'',
          fileLink:''
        })
      } else {
        this.application.splice(index, 1);
      }
    },
    handleSuccess2 (file, uploadFiles,index) {
        console.log(file, 111);
        console.log(index);
        this.application[index].fileLink = file.fileName
    },
    btnContent3(type, index) {
      if (type == 2) {
        this.handle.push({
          value:'',
          fileLink:''
        })
      } else {
        this.handle.splice(index, 1);
      }
    },
    handleSuccess3(file, uploadFiles,index) {
        console.log(file, 111);
        console.log(index);
        this.handle[index].fileLink = file.fileName
    },
//     handleExceed (files, uploadFiles) {
//     this.$message({
//           message: '附件上传数量超出限制',
//           type: 'warning'
//         });
// }
  },
};
</script>
